//
//  FindCollectionViewCell.m
//  LiMinProject
//
//  Created by FM123 on 2017/5/2.
//  Copyright © 2017年 LiMin. All rights reserved.
//

#import "FindCollectionViewCell.h"
#import "ItemsViewController.h"
@implementation FindCollectionViewCell
{
    UIImageView *postBGView;
     BOOL drawed;
}
-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        self.opaque=YES;
        
       self.layer.borderColor=UIColorFromRGB(0xffffff).CGColor;
        [self addSubview:[self setPageViewControllers]];
        
    }
    
    return self;
}
-(UIView *)setPageViewControllers
{
    if (!_RCSegView) {
        ItemsViewController *sceneVC=[[ItemsViewController alloc]init];
        
        ItemsViewController *sceneVC1=[[ItemsViewController alloc]init];
        
        NSArray *controllers=@[sceneVC,sceneVC1];
        NSArray *titleArray =@[@"page1",@"page2"];
        
        CustomSegmentView * rcs=[[CustomSegmentView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) controllers:controllers titleArray:titleArray ParentController:self.superview.viewController lineWidth:WIDTH/5 lineHeight:3. andSelect:0];
        rcs.backgroundColor=[UIColor whiteColor];
        //        rcs.delegate=self;
        _RCSegView = rcs;
    }
    return _RCSegView;
}
/*
 -(id)initWithFrame:(CGRect)frame{
 self=[super initWithFrame:frame];
 if (self) {
 self.opaque=YES;
 
 //        self.layer.borderColor=UIColorFromRGB(0xffffff).CGColor;
 //        self.layer.masksToBounds=YES;
 //        self.clipsToBounds = YES;
 self.backgroundColor=[UIColor whiteColor];
 postBGView = [[UIImageView alloc] initWithFrame:CGRectZero];
 postBGView.backgroundColor=[UIColor whiteColor];
 [self.contentView insertSubview:postBGView atIndex:0];
 
 self.showImg=[[UIImageView alloc]init];
 self.showImg.backgroundColor=[UIColor whiteColor];
 [self.contentView addSubview:self.showImg];
 }
 
 return self;
 }
 - (void)setModel:(NewGoodsModel *)model{
 _model=model;
 
 }
 - (void)setSceneDetailsModel:(SceneDetailsModel *)sceneDetailsModel{
 _sceneDetailsModel=sceneDetailsModel;
 
 }
 -(void)clear;{
 if (!drawed) {
 return;
 }
 postBGView.frame = CGRectZero;
 postBGView.image = nil;
 drawed = NO;
 }
 -(void)drawItems{
 //    [super drawRect:rect];
 if (drawed) {
 return;
 }
 drawed = YES;
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
 //        UIImage *temp = UIGraphicsGetImageFromCurrentImageContext();
 CGRect rect = CGRectMake(0, 0, self.width, _model.cellHeight);
 
 UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
 //        [[UIColor whiteColor] set];
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
 CGContextFillRect(context, rect);
 
 CGRect titleSize=[Helper getTextFrameWithText:_model.name withSize:CGSizeMake(self.width-20, 30) withFont:Font_Semibold(13)];
 CGFloat X=(self.width-20-titleSize.size.width)/2;
 CGFloat Y=_model.cellHeight-kPercenX_scale(65)+10;
 
 [_model.name drawTextWithRect:CGRectMake(X, Y, (self.width-X*2), titleSize.size.height-5) color:Color_333333 context:context font:Font_Semibold(13) alignment:center];
 X=(self.width-kPercenX_scale(130))/2;
 Y=Y+titleSize.size.height;
 //        WWLog(@"X  %f  Y  %f",X,Y);
 [self toDrawLineFromX:X Y:Y toX:X+kPercenX_scale(130) toY:Y context:context color:@"0Xe5e5e5"];
 NSString *priceStr;
 if ((ex_identityType==ex_identityTypeDesigner||ex_identityType==ex_identityTypeCompany))
 {
 priceStr=[NSString stringWithFormat:@"RMB:%@起",[Helper getNewStringWith:_model.vipPrice]];
 }else //if (ex_identityType==ex_identityTypeDesigner||ex_identityType==ex_identityTypeCompany)
 {
 priceStr=[NSString stringWithFormat:@"RMB:%@起",[Helper getNewStringWith:_model.price]];
 
 
 }
 CGRect priceSize=[Helper getTextFrameWithText:priceStr withSize:CGSizeMake(self.width, 30) withFont:Font_Semibold(13)];
 X=(self.width-20-priceSize.size.width)/2;
 Y=Y+5;
 //         WWLog(@"priceSize X  %f  Y  %f",X,Y);
 
 [priceStr drawTextWithRect:CGRectMake(X, Y, (self.width-X*2), titleSize.size.height-5) color:Color_fac444 context:context font:Font_Bold(13) alignment:center];
 CGRect freeSize=[Helper getTextFrameWithText:@"现货包邮" withSize:CGSizeMake(1000, 30) withFont:Font_Semibold(13)];
 X=(self.width-20-freeSize.size.width)/2;
 Y=Y+priceSize.size.height;
 //        WWLog(@"freeSize X  %f  Y  %f",X,Y);
 
 [@"现货包邮" drawTextWithRect:CGRectMake(X, Y, (self.width-X*2), titleSize.size.height-5) color:UIColorFromRGB(0X666666) context:context font:Font_Semibold(11) alignment:center];
 UIImage *temp1 = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 dispatch_async(dispatch_get_main_queue(), ^{
 
 postBGView.frame = rect;
 postBGView.image = nil;
 postBGView.image = temp1;
 
 });
 });
 }
 -(void)drawScene{
 if (drawed) {
 return;
 }
 drawed = YES;
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
 
 CGRect rect = CGRectMake(0, 0, self.width, _sceneDetailsModel.cellHeight);
 
 UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
 CGContextFillRect(context, rect);
 
 CGRect titleSize=[Helper getTextFrameWithText:_sceneDetailsModel.name withSize:CGSizeMake(self.width-20, 30) withFont:Font_Semibold(13)];
 CGFloat X=10;
 CGFloat Y=_sceneDetailsModel.cellHeight-kPercenX_scale(65)+15;
 
 [_sceneDetailsModel.name drawTextWithRect:CGRectMake(X, Y, self.width-20, titleSize.size.height-5) color:Color_333333 context:context font:Font_Semibold(13) alignment:left];
 
 NSString *priceStr;
 if ((ex_identityType==ex_identityTypeDesigner||ex_identityType==ex_identityTypeCompany))
 {
 priceStr=[NSString stringWithFormat:@"RMB:%@起",[Helper getNewStringWith:_sceneDetailsModel.vipPrice]];
 }else //if (ex_identityType==ex_identityTypeDesigner||ex_identityType==ex_identityTypeCompany)
 {
 priceStr=[NSString stringWithFormat:@"RMB:%@起",[Helper getNewStringWith:_sceneDetailsModel.price]];
 
 }
 CGRect priceSize=[Helper getTextFrameWithText:priceStr withSize:CGSizeMake(self.width, 30) withFont:Font_Bold(14)];
 X=10;
 Y=_sceneDetailsModel.cellHeight-15-priceSize.size.height+5;
 //        WWLog(@"priceSize X  %f  Y  %f",X,Y);
 
 [priceStr drawTextWithRect:CGRectMake(X, Y,self.width-20, titleSize.size.height-5) color:Color_fac444 context:context font:Font_Bold(14) alignment:left];
 
 NSString *strCount=[NSString stringWithFormat:@"%@件",_sceneDetailsModel.goodsAmout];
 CGRect countBtnSize=[Helper getTextFrameWithText:strCount withSize:CGSizeMake(self.width, 30) withFont:Font_Semibold(12)];
 UIImage *image = [UIImage imageNamed:@"物品"];
 X=self.width-(countBtnSize.size.width+11+5)-10;
 Y=Y+(titleSize.size.height-5-image.size.height)/2;
 
 [image drawInRect:CGRectMake(X, Y, image.size.width, image.size.height)];
 
 X=X+image.size.width+10;
 Y=Y+(image.size.height+5-countBtnSize.size.height)/2;
 //        WWLog(@"freeSize X  %f  Y  %f",X,Y);
 
 [strCount drawTextWithRect:CGRectMake(X, Y, countBtnSize.size.width, countBtnSize.size.height-5) color:UIColorFromRGB(0x999999) context:context font:Font_Semibold(12) alignment:center];
 
 UIImage *temp1 = UIGraphicsGetImageFromCurrentImageContext();
 
 UIGraphicsEndImageContext();
 dispatch_async(dispatch_get_main_queue(), ^{
 
 postBGView.frame = rect;
 postBGView.image = nil;
 postBGView.image = temp1;
 
 });
 });
 //
 }
 // 绘制直线
 - (void)toDrawLineFromX:(CGFloat)x1 Y:(CGFloat)y1 toX:(CGFloat)x2 toY:(CGFloat)y2 context:(CGContextRef)con color:(NSString *)color{
 CGContextMoveToPoint(con, x1, y1);
 
 NSDictionary *dic=[color colorWithHexString:color];
 CGContextSetRGBStrokeColor(con,[dic[@"r"] floatValue]/255.0, [dic[@"g"] floatValue]/255.0, [dic[@"b"] floatValue]/255.0, 1.0);
 //设置填充颜色229
 //    CGContextSetRGBFillColor(con, [dic[@"r"] floatValue]/255.0, [dic[@"g"] floatValue]/255.0, [dic[@"b"] floatValue]/255.0, 1.0);
 CGContextAddLineToPoint(con, x2, y2);
 CGContextSetLineWidth(con, 1);
 CGContextStrokePath(con);
 }
 ///绘制文字，rect1指定矩形，绘制文字在这个矩形水平和垂直居中
 - (void)toDrawTextWithRect:(CGRect)rect1 str:(NSString*)str1 context:(CGContextRef)context font:(UIFont *)font{
 if( str1 == nil || context == nil)
 return;
 
 CGContextSetLineWidth(context, 1.0);
 CGContextSetRGBFillColor (context, 0.01, 0.01, 0.01, 1);
 //    CGContextsetc
 //段落格式
 NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
 textStyle.lineBreakMode = NSLineBreakByWordWrapping;
 textStyle.alignment = NSTextAlignmentCenter;//水平居中
 //字体
 //    UIFont  *font = font;
 //构建属性集合
 NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:textStyle};
 //获得size
 CGSize strSize = [str1 sizeWithAttributes:attributes];
 CGFloat marginTop = (rect1.size.height - strSize.height)/2;
 //垂直居中要自己计算
 CGRect r = CGRectMake(rect1.origin.x, rect1.origin.y + marginTop,rect1.size.width, strSize.height);
 [str1 drawInRect:r withAttributes:attributes];
 }
 */



@end
